package com.ryhx.util;

public class MysqlUtil {
	public static String TYPE_NAME_TEXT = "text";//大字段
	public static String TYPE_NAME_VARCHAR = "varchar";//字符串
	public static String TYPE_NAME_BIGINT = "bigint";//大整数
	public static String TYPE_NAME_INT = "int";//整数
	public static String TYPE_NAME_DOUBLE = "double";//双精度类型
	public static String TYPE_NAME_DECIMAL = "decimal";//decimal
	public static String TYPE_NAME_DATETIME = "datetime";//年月日 + 时分秒
	public static String TYPE_NAME_DATE = "date";//年月日
	public static String TYPE_NAME_CHAR = "char";//字符
	public static String TYPE_NAME_BIGINT_UNSIGNED = "bigint unsigned";//无标志大整数
	public static String TYPE_NAME_INT_UNSIGNED = "int unsigned";//无标志整数
	/**
	 * 获取 列类型 字符串
	 * @param columnType
	 * @param datasize
	 * @param digits
	 * @return
	 */
	public static String getColumnTypeStr(String columnType, int datasize,int digits){
		columnType = columnType.toLowerCase();
		if(TYPE_NAME_TEXT.equals(columnType)){
			return " " + columnType;
		}else if(TYPE_NAME_VARCHAR.equals(columnType)){
			return " " + columnType + "(" + datasize  + ")";
		}else if(TYPE_NAME_BIGINT_UNSIGNED.equals(columnType)){
			return " " + TYPE_NAME_BIGINT + "(" + (datasize + 1)  + ")";
		}else if(TYPE_NAME_BIGINT.equals(columnType)){
			return " " + columnType + "(" + (datasize + 1)  + ")";
		}else if(TYPE_NAME_INT.equals(columnType)){
			return " " + columnType + "(" + datasize  + ")";
		}else if(TYPE_NAME_INT_UNSIGNED.equals(columnType)){
			return " " + TYPE_NAME_INT + "(" + datasize  + ")";
		}else if(TYPE_NAME_DECIMAL.equals(columnType)){
			return " " + columnType + "(" + datasize + "," + digits + ")";
		}else if(TYPE_NAME_DATETIME.equals(columnType)){
			return " " + columnType;
		}else if(TYPE_NAME_DATE.equals(columnType)){
			return " " + columnType;
		}else if(TYPE_NAME_CHAR.equals(columnType)){
			return " " + columnType + "(" + datasize  + ")";
		}else if(TYPE_NAME_DOUBLE.equals(columnType)){
			return " " + columnType + "(" + datasize  + ")";
		}else{
			try {
				throw new Exception(columnType + "数据类型未集成到系统，请集成！");
			} catch (Exception e) {
				System.out.println(e.getMessage());
				System.exit(0);
			}
		}
		return "";
	}
	/**
	 * 获取 列是否为空 字符串
	 * 1 可以为空  0 不能为空
	 * @param nullable
	 * @return
	 */
	public static String getNullableStr(int nullable){
		return nullable == 0 ? " NOT NULL " : " ";
	}
	/**
	 * 获取 列是否自增 字符串
	 * YES自增  NO非自增
	 * @param nullable
	 * @return
	 */
	public static String getAutoIncrement(String autoIncrement){
		return "YES".equals(autoIncrement) ? " AUTO_INCREMENT " : " ";
	}
	/**
	 * 获取 列备注 字符串
	 * @param remarks
	 * @return
	 */
	public static String getColumnRemarks(String remarks){
		if(null == remarks || "".equals(remarks)){
			return "";
		}else{
			return  " COMMENT " + "'" + remarks + "' ";
		}
	}
	/**
	 * 获取列的默认值
	 * @param columnDefault
	 * @param columnDefault 
	 * @return
	 */
	public static String getColumnDefaultStr(String columnType, String columnDefault){
		if(TYPE_NAME_DATETIME.equals(columnType.toLowerCase()) || TYPE_NAME_DATE.equals(columnType.toLowerCase())){
			return "";
		}
		if(null == columnDefault){
			return " DEFAULT NULL";
		}else if("".equals(columnDefault)){
			return " DEFAULT  ''";
		}else if(null != columnDefault && !"".equals(columnDefault)){
			return " DEFAULT " + columnDefault;
		}else{
			try {
				throw new Exception(columnDefault + "数据默认类型未集成到系统，请集成！");
			} catch (Exception e) {
				System.out.println(e.getMessage());
				System.exit(0);
			}
		}
		return "";
	}
	/**
	 * 当 插入数据库时  列的默认值
	 * @param columnDefault
	 * @param columnDefault 
	 * @return
	 */
	public static String getColumnInsertDefaultStr(String columnType, String columnDefault){
		columnType = columnType.toLowerCase();
		if(TYPE_NAME_DATETIME.equals(columnType) || TYPE_NAME_DATE.equals(columnType)){
			return "NULL";
		}
		if(TYPE_NAME_DOUBLE.equals(columnType) || TYPE_NAME_INT.equals(columnType) || TYPE_NAME_BIGINT.equals(columnType) || TYPE_NAME_DECIMAL.equals(columnType)){
			if(null != columnDefault && !"".equals(columnDefault)){
				return "'"+ columnDefault + "'";
			}
		}
		if(null == columnDefault){
			return "NULL";
		}else if("".equals(columnDefault)){
			return "''";
		}else if(null != columnDefault && !"".equals(columnDefault)){
			return columnDefault;
		}else{
			try {
				throw new Exception(columnDefault + "数据默认值未集成到系统，请集成！");
			} catch (Exception e) {
				System.out.println(e.getMessage());
				System.exit(0);
			}
		}
		return "";
	}
	
}
